// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/testing/connectors/fuzzer_connector.h>
#include <pollux/testing/exec/util/operator_test_base.h>
#include <pollux/vector/vector_fuzzer.h>

namespace kumo::pollux::connector::fuzzer::test {

class FuzzerConnectorTestBase : public exec::test::OperatorTestBase {
 public:
  const std::string kFuzzerConnectorId = "test-fuzzer";

  void SetUp() override {
    OperatorTestBase::SetUp();
    connector::registerConnectorFactory(
        std::make_shared<connector::fuzzer::FuzzerConnectorFactory>());
    std::shared_ptr<const config::ConfigBase> config;
    auto fuzzerConnector =
        connector::getConnectorFactory(
            connector::fuzzer::FuzzerConnectorFactory::kFuzzerConnectorName)
            ->newConnector(kFuzzerConnectorId, config);
    connector::registerConnector(fuzzerConnector);
  }

  void TearDown() override {
    connector::unregisterConnector(kFuzzerConnectorId);
    connector::unregisterConnectorFactory(
        connector::fuzzer::FuzzerConnectorFactory::kFuzzerConnectorName);
    OperatorTestBase::TearDown();
  }

  exec::Split makeFuzzerSplit(size_t numRows) const {
    return exec::Split(
        std::make_shared<FuzzerConnectorSplit>(kFuzzerConnectorId, numRows));
  }

  std::vector<exec::Split> makeFuzzerSplits(
      size_t rowsPerSplit,
      size_t numSplits) const {
    std::vector<exec::Split> splits;
    splits.reserve(numSplits);

    for (size_t i = 0; i < numSplits; ++i) {
      splits.emplace_back(makeFuzzerSplit(rowsPerSplit));
    }
    return splits;
  }

  std::shared_ptr<FuzzerTableHandle> makeFuzzerTableHandle(
      size_t fuzzerSeed = 0) const {
    return std::make_shared<FuzzerTableHandle>(
        kFuzzerConnectorId, fuzzerOptions_, fuzzerSeed);
  }

 private:
  VectorFuzzer::Options fuzzerOptions_;
};

} // namespace kumo::pollux::connector::fuzzer::test
